#include "stdafx.h"

#include "FindImageBlot.h"


void FindImageEdgelib::FindImageBlot ::Fun ( long i, long j)
{
	
	if(i<this->m_SX || i>this->m_EX || j<this->m_SY || j> this->m_EY )
		return;
	
	/*if(N>180000)
	{
		N--;
		return;
	}	*/
	

	if(this->m_ImageArray [i,j]>this->m_Gray )
	{
		if (this->m_Flag ==true)
		{
			m_Counts++;
			this->m_ImageArray [i,j]=m_Gray;
		}
		else
		{
			//this->m_ImageArray [i,j]=m_Gray;
			return;
		}	

		if (m_Counts==1)
		{
			m_MinX=i;
			m_MinY=j;
		}

		if (i> m_MaxX)
		  m_MaxX=i;

		if (i<m_MinX)
			m_MinX=i;
		
		if (j>m_MaxY)
			m_MaxY=j;

		if (j<m_MinY)
			m_MinY=j;

		N++;
		if(N>=40000)
		{
			ExitCounts++;
			if(ExitCounts>=10)
			{
				N=N-ExitCounts;
				ExitCounts=0;

			}	
			return;
		}

		
		for(Ti=i-1;Ti<=i+1;Ti++)
		{
			for(Tj=j-1;Tj<=j+1;Tj++)
			{
				if (Ti!=i || Tj!=j)
				    Fun(Ti,Tj);
			}
		}

	}
	else if(this->m_ImageArray [i,j]<this->m_Gray )
	{

		if (this->m_Flag ==false)
		{
			m_Counts++;
			this->m_ImageArray [i,j]=m_Gray;
		}
		else
		{
			//this->m_ImageArray [i,j]=m_Gray;
			return;
		}	

		if (m_Counts==1)
		{
			m_MinX=i;
			m_MinY=j;
		}

		if (i> m_MaxX)
		    m_MaxX=i;

		if (i<m_MinX)
			m_MinX=i;
		
		if (j>m_MaxY)
			m_MaxY=j;

		if (j<m_MinY)
			m_MinY=j;

		N++;

		if(N>=40000)
		{
			ExitCounts++;
			if(ExitCounts>=10)
			{
				N=N-ExitCounts;
				ExitCounts=0;

			}	
			return;
		}
		
		for(Ti=i-1;Ti<=i+1;Ti++)
		{
			for(Tj=j-1;Tj<=j+1;Tj++)
			{
				if (Ti!=i || Tj!=j)
				    Fun(Ti,Tj);
			}
		}

	}

	//this->m_ImageArray [i,j]=m_Gray;
	return;

	

       
}


bool FindImageEdgelib::FindImageBlot ::Runing(long X,long Y,cli::array<long,1> ^result) 
{
	this->m_Counts =0;
	this->m_MinX=0;
	this->m_MaxX=0;
	this->m_MinY=0;
	this->m_MaxY=0;
	this->Fun (X,Y);
	N=0;
	ExitCounts=0;
	if(m_Counts>=this->m_Area )
	{
		result[0]=this->m_MinX ;
		result[1]=this->m_MaxX ;
		result[2]=this->m_MinY ;
		result[3]=this->m_MaxY ;
		result[4]=this->m_Counts ;
		return true;
	}
	return false;
}